import streamlit as st
from file_operations import FileHandler
import os
# 添加自定义CSS样式
st.markdown('''
<style>
    .stTextInput > div > div > input {
        border: 1px solid #ccc;
        border-radius: 4px;
        padding: 8px;
    }
    .stButton > button {
        background-color: #008CBA;
        color: white;
        border-radius: 4px;
        padding: 8px 16px;
    }
    .stButton > button:hover {
        background-color: #005f73;
    }
    .stHeader {
        color: #008CBA;
    }
</style>
''', unsafe_allow_html=True)

# 使用侧边栏输入Webhook URL
with st.sidebar:
    webhook_url = st.text_input('请输入飞书 Webhook URL', 'https://open.feishu.cn/open-apis/bot/v2/hook/86bd9e6d-605d-4031-a27e-a347f5aaa3f9')
    handler = FileHandler(webhook_url)
    # 重命名文件部分
    st.header('重命名文件')
    rename_folder_path = st.text_input('请输入要重命名文件的文件夹路径', './a')
    st.subheader('文件夹文件列表')
    files = os.listdir(rename_folder_path)
    files_placeholder = st.container(border=True, height=500)
    for file in files:
        files_placeholder.markdown(file)
    if st.button('执行重命名'):
        files_placeholder = st.empty()
        handler.rename_files(rename_folder_path)
        st.success('重命名操作已提交')
        files = os.listdir(rename_folder_path)
        for file in files:
            files_placeholder.markdown(file)
        
        

# 创建 FileHandler 实例




# 移动文件部分

st.header('移动文件')
source_folder = st.text_input('请输入源文件夹路径', './a')
target_folder = st.text_input('请输入目标文件夹路径', './b')
num_files = st.number_input('请输入要移动的文件数量', value=2, min_value=1)

# 显示源文件夹和目标文件夹的文件列表
source_files = os.listdir(source_folder) if os.path.exists(source_folder) else []
target_files = os.listdir(target_folder) if os.path.exists(target_folder) else []

left_col, arrow_col, right_col = st.columns([3, 1, 3])

with left_col:
    st.subheader('源文件夹文件')
    source_files_placeholder = st.container(border=True, height=300)
    for file in source_files:
        source_files_placeholder.markdown(file)

with arrow_col:
    st.markdown("<h1 style='text-align: center;'>→</h1>", unsafe_allow_html=True)

with right_col:
    st.subheader('目标文件夹文件')
    target_files_placeholder = st.container(border=True, height=300)
    for file in target_files:
        target_files_placeholder.markdown(file)

if st.button('执行移动'):
    message = handler.move_files(source_folder, target_folder, num_files)
    # st.success('移动操作已提交')
    st.rerun()
    st.success(message)
    